package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TourismUser;
import com.ruoyi.system.mapper.TourismUserMapper;
import com.sun.jna.platform.unix.solaris.LibKstat;
import javafx.scene.AmbientLight;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismSpot;
import com.ruoyi.system.service.ITourismSpotService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 景点Controller
 *
 * @author gqd
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/system/place")
public class TourismSpotController extends BaseController
{
    @Autowired
    private ITourismSpotService tourismSpotService;
    @Autowired
    private TokenService tokenService;
    @Resource
    private TourismUserMapper tourismUserMapper;

    /**
     * 查询景点列表
     */
    @PreAuthorize("@ss.hasPermi('system:place:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismSpot tourismSpot)
    {
        startPage();
        List<TourismSpot> list = tourismSpotService.selectTourismSpotList(tourismSpot);
        return getDataTable(list);
    }

    /**
     * 导出景点列表
     */
    @PreAuthorize("@ss.hasPermi('system:place:export')")
    @Log(title = "景点", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismSpot tourismSpot)
    {
        List<TourismSpot> list = tourismSpotService.selectTourismSpotList(tourismSpot);
        ExcelUtil<TourismSpot> util = new ExcelUtil<TourismSpot>(TourismSpot.class);
        return util.exportExcel(list, "place");
    }

    /**
     * 获取景点详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:place:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(tourismSpotService.selectTourismSpotById(id));
    }

    /**
     * 新增景点
     */
    @PreAuthorize("@ss.hasPermi('system:place:add')")
    @Log(title = "景点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismSpot tourismSpot)
    {
        return toAjax(tourismSpotService.insertTourismSpot(tourismSpot));
    }

    /**
     * 修改景点
     */
    @PreAuthorize("@ss.hasPermi('system:place:edit')")
    @Log(title = "景点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismSpot tourismSpot)
    {
        return toAjax(tourismSpotService.updateTourismSpot(tourismSpot));
    }

    /**
     * 删除景点
     */
    @PreAuthorize("@ss.hasPermi('system:place:remove')")
    @Log(title = "景点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tourismSpotService.deleteTourismSpotByIds(ids));
    }


    /**
     * 精选景点的查询
     * @param map
     * @return
     */
    @PostMapping("/selectSpotByName")
    public AjaxResult selectSpotByName(@RequestBody Map map){
        String spotName = (String) map.get("spotName");
        return tourismSpotService.selectSpotByName(spotName);
    }

    /**
     * 根据景点id查询景点详细信息
     * @param
     * @return
     */
    @PostMapping("/selectSpotById")
    public AjaxResult selectSpotById(@RequestBody Map map, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser != null){
            Integer userId = loginUser.getUserId();
            Integer spotId = (Integer) map.get("spotId");
            return tourismSpotService.selectSpotById(spotId,userId);
        }else {
            Integer userId = null;
            Integer spotId = (Integer) map.get("spotId");
            return tourismSpotService.selectSpotById(spotId,userId);
        }

    }
}
